// MIDI_to_MFSF converter.cpp : Defines the entry point for the console application.
//
#include "MIDIObj.h"
#include <cstdio>

int PrintTrackToFile(int channel, unsigned long* buffer, int len, int mtrkNum)
{
	int i;
	FILE* ofptr;
	char temp[100000];

	if(len == 0) return -1;

	sprintf(temp, "./DataOut/mtrk Track %d - Channel %d Out.mfs", mtrkNum, channel);

	fopen_s(&ofptr, temp , "w+");

	if(!ofptr)
		return -1;

	i = 0;
	while( (buffer[i] != 0) && (i < len) )
	{
		fprintf(ofptr, "%u\n", buffer[i]);
		i++;
	}

	fclose(ofptr);

	return 0;
}

int StripAndPrintNotes(MIDIObj* theObj, unsigned long *finalBuff, int buffLen, int mtrkNum)
{
	theObj->StripChannelNotes(0, finalBuff, buffLen);
	PrintTrackToFile(0, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(1, finalBuff, buffLen);
	PrintTrackToFile(1, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(2, finalBuff, buffLen);
	PrintTrackToFile(2, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(3, finalBuff, buffLen);
	PrintTrackToFile(3, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(4, finalBuff, buffLen);
	PrintTrackToFile(4, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(5, finalBuff, buffLen);
	PrintTrackToFile(5, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(6, finalBuff, buffLen);
	PrintTrackToFile(6, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(7, finalBuff, buffLen);
	PrintTrackToFile(7, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(8, finalBuff, buffLen);
	PrintTrackToFile(8, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(9, finalBuff, buffLen);
	PrintTrackToFile(9, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(10, finalBuff, buffLen);
	PrintTrackToFile(10, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(11, finalBuff, buffLen);
	PrintTrackToFile(11, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(12, finalBuff, buffLen);
	PrintTrackToFile(12, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(13, finalBuff, buffLen);
	PrintTrackToFile(13, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(14, finalBuff, buffLen);
	PrintTrackToFile(14, finalBuff, buffLen, mtrkNum);

	theObj->StripChannelNotes(15, finalBuff, buffLen);
	PrintTrackToFile(15, finalBuff, buffLen, mtrkNum);

	return 0;
}

int main(int argc, char* argv[])
{
	unsigned int i;
	int mtrkOffset;
	int totalMTRK;
	unsigned long lengthOfFile;
	const int mtrkLen = 8;
	unsigned char toRead;
	unsigned char totalBuff[100000];
	unsigned long finalBuff[100000];
	unsigned char* track;
	unsigned long len;
	FILE* theFile;
	MTHDCHK* mthd;
	MTRKCHK* mtrk;
	MIDIObj* theObject;

	fopen_s(&theFile, "canonstr.mid", "rb");
	if(theFile == 0)
		return -1;

	i = 0;
	
	while(!feof(theFile))
	{
		toRead = getc(theFile);
		totalBuff[i++] = toRead;
	}

	lengthOfFile = i;
	len = 0;
	mtrkOffset = 14;

	mthd = (MTHDCHK*) totalBuff;
	totalMTRK = (mthd->numTracks[0] << 8) + mthd->numTracks[1];

	for(i = 0; (i < totalMTRK) && (len < lengthOfFile); i++)
	{
		mtrk = (MTRKCHK*) &totalBuff[mtrkOffset];
	
		track = &totalBuff[mtrkOffset + mtrkLen];
		len = mtrk->length[3] + (mtrk->length[2] << 8) + (mtrk->length[1] << 16) + (mtrk->length[0] << 24);

		if((len + mtrkOffset + mtrkLen) >= lengthOfFile)
			len = lengthOfFile - (mtrkOffset + mtrkLen);

		theObject = new MIDIObj(track, len);
	
		StripAndPrintNotes(theObject, finalBuff, 1000, i);
		mtrkOffset += mtrkLen + len;
	}

	fclose(theFile);

	return 0;
}

